package com.sj.service.impl;

import com.sj.entity.Menu;
import com.sj.mapper.MenuMapper;
import com.sj.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sj.utils.RedisUtil;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Value("${sjblog.key.menuListKey}")
    private String menuListKey;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<Menu> getMenuListByUserId(Integer userId) {
        return menuMapper.getMenuListByUserId(userId);
    }

    @Override
    public List<Menu> getAllMenuList(Integer parentId) {
        if (redisUtil.hasKey(menuListKey)) {
            return (List<Menu>) redisUtil.get(menuListKey);
        }
        List<Menu> menuList = menuMapper.getAllMenuList(parentId);
        redisUtil.set(menuListKey, menuList);
        return menuList;
    }
}
